package com.alibaba.citrus.cr.platform.enhance.facade.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.platform.enhance.functions.dimension.entity.DimensionEntity;
import com.alibaba.citrus.cr.platform.enhance.functions.dimension.factory.DimensionFactory;
import com.alibaba.citrus.cr.platform.enhance.repository.DimensionQueryRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.api.dimensionwrite.service.DimensionWriteService;
import com.epoch.app.crplatformenhance.dimension.dto.QueryListByPriorityRequest;
import com.epoch.app.crplatformenhance.dimension.model.Dimension;
import com.epoch.app.crplatformenhance.model.dto.*;
import com.epoch.app.crplatformenhance.sdo.DimensionSDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
@Primary
public class DimensionWriteServiceImpl implements DimensionWriteService {

    @Resource
    private DimensionQueryRepository dimensionQueryRepository;

    @Override
    @FacadeInvoker
    public Result<DimensionWriteUpdateResponse> updateDimension(DimensionWriteUpdateRequest request) {
        DimensionEntity dimensionEntity = DimensionFactory.updateDimension(request);
        String id = dimensionEntity.update();
        DimensionWriteUpdateResponse dimensionWriteUpdateResponse = new DimensionWriteUpdateResponse();
        dimensionWriteUpdateResponse.setId(id);
        return Result.success(dimensionWriteUpdateResponse);
    }

    @Override
    @FacadeInvoker
    public Result<DimensionWriteDeleteResponse> deleteDimension(DimensionWriteDeleteRequest request) {
        DimensionEntity dimensionEntity = DimensionFactory.deleteDimensionStatus(request);
        String id = dimensionEntity.delete();
        DimensionWriteDeleteResponse response = new DimensionWriteDeleteResponse(id);
        return Result.success(response);
    }


    @Override
    @FacadeInvoker
    public Result<DimensionWriteCreateResponse> createDimension(DimensionWriteCreateRequest dimensionWriteCreateRequest) {
        return null;
    }

    @Override
    @FacadeInvoker
    public Result<Boolean> updateStatus(UpdateDimensionStatusRequest updateDimensionStatusRequest) {
        DimensionEntity dimensionEntity = DimensionFactory.updateStatus(updateDimensionStatusRequest);
        dimensionEntity.update();
        return Result.success(true);
    }

    @Override
    public Result<Boolean> updatePriority(UpdatePriorityRequest updatePriorityRequest) {
        List<DimensionSDO> dimensionSDOS = new ArrayList<>();
        if ("up".equals(updatePriorityRequest.getSign())) {
            DimensionSDO dimensionSDO = dimensionQueryRepository.query(updatePriorityRequest.getId());
            Integer priority = dimensionSDO.getPriority();
            if (null == priority || 1 == priority) {
                return Result.fail("999", "无法上移");
            }
            dimensionSDO.setPriority(priority - 1);
            dimensionSDOS.add(dimensionSDO);

            DimensionPageQueryRequest pageQueryRequest = new DimensionPageQueryRequest();
            pageQueryRequest.setPriority(priority - 1);
            pageQueryRequest.setStrategyId(updatePriorityRequest.getStrategyId());
            pageQueryRequest.setDimensionCode(updatePriorityRequest.getCode());
            Result<List<DimensionPageQueryResponse>> listResult = dimensionQueryRepository.pageQuery(pageQueryRequest);
            DimensionPageQueryResponse dimensionPageQueryResponse = listResult.getResult().get(0);
            DimensionSDO query = dimensionQueryRepository.query(dimensionPageQueryResponse.getId());
            query.setPriority(priority);
            dimensionSDOS.add(query);

            for (DimensionSDO sdo : dimensionSDOS) {
                DimensionEntity downDimensionEntity = DimensionFactory.updatePriority(sdo);
                downDimensionEntity.updatePriority();
            }
            return Result.success(true);
        }

        //下移

        DimensionSDO dimensionSDO = dimensionQueryRepository.query(updatePriorityRequest.getId());

        Integer priority = dimensionSDO.getPriority();
        dimensionSDO.setPriority(priority + 1);
        dimensionSDOS.add(dimensionSDO);

        DimensionPageQueryRequest pageQueryRequest = new DimensionPageQueryRequest();
        pageQueryRequest.setPriority(priority + 1);
        pageQueryRequest.setStrategyId(updatePriorityRequest.getStrategyId());
        pageQueryRequest.setDimensionCode(updatePriorityRequest.getCode());
        Result<List<DimensionPageQueryResponse>> listResult = dimensionQueryRepository.pageQuery(pageQueryRequest);
        if (CollectionUtils.isEmpty(listResult.getResult())) {
            return Result.fail("999", "无法下移");
        }

        DimensionPageQueryResponse dimensionPageQueryResponse = listResult.getResult().get(0);
        DimensionSDO query = dimensionQueryRepository.query(dimensionPageQueryResponse.getId());
        query.setPriority(priority);
        dimensionSDOS.add(query);
        for (DimensionSDO sdo : dimensionSDOS) {
            DimensionEntity downDimensionEntity = DimensionFactory.updatePriority(sdo);
            downDimensionEntity.updatePriority();
        }
        return Result.success(true);
    }


    @Override
    public Result<Boolean> deleteByPriority(DeleteByPriorityRequest deleteByPriorityRequest) {
        QueryListByPriorityRequest queryRequest = new QueryListByPriorityRequest();
        queryRequest.setCode(deleteByPriorityRequest.getCode());
        queryRequest.setStrategyId(deleteByPriorityRequest.getStrategyId());
        Result<List<Dimension>> listResult = dimensionQueryRepository.queryListByPriority(queryRequest);
        List<Dimension> result = listResult.getResult();
        if (result.size() == 1 || result.get(result.size() - 1).getId().equals(Long.parseLong(deleteByPriorityRequest.getId())) ) {
            DimensionWriteDeleteRequest deleteRequest = new DimensionWriteDeleteRequest();
            deleteRequest.setId(deleteByPriorityRequest.getId());
            DimensionEntity dimensionEntity = DimensionFactory.deleteDimension(deleteRequest);
            dimensionEntity.delete();
            return Result.success(true);
        }

        for (int i = 0; i < result.size(); i++) {
            if (result.get(i).getId().toString().equals(deleteByPriorityRequest.getId())) {
                DimensionSDO dimensionSDO = new DimensionSDO();
                dimensionSDO.setId(Long.parseLong(deleteByPriorityRequest.getId()));
                dimensionSDO.setPriority(result.get(i).getPriority());
                dimensionSDO.setStrategyId(deleteByPriorityRequest.getStrategyId());
                dimensionSDO.setCode(deleteByPriorityRequest.getCode());
                DimensionEntity deleteEntity = DimensionFactory.updatePriority(dimensionSDO);
                deleteEntity.deleteByPriority();
                break;
            }
        }
        return Result.success(true);
    }
}
